package com.ldzl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ldzl.pojo.CkStorageArea;
import com.ldzl.pojo.CkStorageLocation;
import com.ldzl.service.CkStorageAreaService;
import com.ldzl.mapper.CkStorageAreaMapper;
import com.ruoyi.common.core.domain.UR;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author 风止
* @description 针对表【ck_storage_area(库位表)】的数据库操作Service实现
* @createDate 2025-07-03 09:32:13
*/
@Service
public class CkStorageAreaServiceImpl extends ServiceImpl<CkStorageAreaMapper, CkStorageArea>
    implements CkStorageAreaService{

    /**
     * 条件分页查询指定库区的库位
     * @param area
     * @return
     */
    @Override
    public List<CkStorageArea> findArea(CkStorageArea area) {
        List<CkStorageArea> listArea = this.baseMapper.selectArea(area);
        System.out.println("测试查询的库位:"+listArea.size());
        for (CkStorageArea are : listArea) {
            //计算库位已用容量百分比
            BigDecimal percentage = are.getCurrent_weight()
                    .divide(are.getMax_weight(), 4, BigDecimal.ROUND_HALF_UP)
                    .multiply(new BigDecimal("100"));

            // 设置保留两位小数
            percentage = percentage.setScale(2, BigDecimal.ROUND_HALF_UP);
            System.out.println("测试容量百分比：" + percentage + "%");
            are.setUsed_capacity(percentage);
        }
        return listArea;
    }

    /**
     * 新增/修改 库位
     * @param area
     * @return
     */
    @Override
    public int  saveArea(CkStorageArea area) {
        //查询名称是否重复
        CkStorageArea area1 = super.getOne(new QueryWrapper<CkStorageArea>()
                .eq("area_name", area.getArea_name())
                .eq("location_id", area.getLocation_id())
                .eq("is_delete",0));

        //查询位置是否重复
        CkStorageArea area_x = super.getOne(new QueryWrapper<CkStorageArea>()
                .eq("position_x", area.getPosition_x())
                .eq("position_y", area.getPosition_z())
                .eq("position_z", area.getPosition_y())
                .eq("location_id", area.getLocation_id())
                .eq("is_delete",0));

        area.setCurrent_weight(new BigDecimal(0.0));
        if(area.getArea_id() == null){
            //新增数据
            if(area1 != null && area_x != null)
                return 5; //库位名称和位置重复

            if(area1 != null)
                return 3; //库位名称重复

            //赋值创建时间和更新时间
            area.setCreate_time(new Date());
            area.setUpdate_time(new Date());
        }else{
            CkStorageArea area2 = super.getOne(new QueryWrapper<CkStorageArea>()
                    .eq("area_id", area.getArea_id())
                    .eq("is_delete",0));
            if(area1 != null && !area1.equals(area2.getArea_name()) && area_x != null)
                return 5; //库位名称和位置重复

            //判断是否重复
            if(area1 != null && !area1.equals(area2.getArea_name()))
                return 3; //库位名称重复

            //修改数据
            area.setUpdate_time(new Date());
            area.setUpdate_by(area.getUpdate_by());
            //清除创建人
            area.setCreate_by(null);
        }
        if (area_x != null)
            return 4; //位置重复

        if(super.saveOrUpdate(area))
            return 1; //成功
        return 2; //失败
    }

    /**
     * 删除库区的库位
     * @param location_id
     * @return
     */
    @Override
    public boolean delete_location_id(Long location_id) {
        return super.baseMapper.delete_location_id( location_id) > 0;
    }

    /**
     * 删除库位
     * @param area_id
     * @return
     */
    @Override
    public boolean deleteArea(Long area_id) {
        CkStorageArea area = new CkStorageArea();
        area.setArea_id(area_id);
        area.setIs_delete("1");
        return super.saveOrUpdate(area);
    }

    /**
     * 批量删除库位
     * @param ids
     * @return
     */
    @Override
    public String deleteArea_batch(List<Long> ids) {
        List<CkStorageArea> listArea = new ArrayList<>();
        for (Long area_id : ids) {
            CkStorageArea area = new CkStorageArea();
            area.setArea_id(area_id);
            area.setIs_delete("1");

            listArea.add(area);
        }
        if (!super.saveOrUpdateBatch(listArea))
            return "fail";
        return "success";
    }
}




